#include <iostream>
#include <vector>

using namespace std;

class UnionFindSet
{
private:
    vector<int> _a;
public:
    UnionFindSet(int n) : _a(n, -1) {}

    void Union(int x, int y)
    {
        int RootX = FindRoot(x);
        int RootY = FindRoot(y);
        if(RootX == RootY) return;
        _a[RootX] += _a[RootY];
        _a[RootY] = RootX;
    }

    int FindRoot(int x)
    {
        if(x >= _a.size()) 
        {
            _a.resize(x+1, -1);
            return x;
        }
        int memo = x;
        while(_a[x] >= 0)
        {
            x = _a[x];
            _a[memo] = x;
        }
        return x;
    }

    int SetSize()
    {
        int ret = 0;
        for(auto& x : _a) ret += (x < 0);
        return ret;
    }

    bool IsUnion(int x, int y)
    {
        return FindRoot(x) == FindRoot(y);
    }
};